import { useModel } from "umi";
import { Form, Button, message, Space, Select } from "antd";
import { useReactive } from "ahooks";
import { useEffect } from "react";

import { UpdateIndustryType } from "../../_graphql/organizations";

const EditIndustryType = ({ record, reloadData }) => {
    const { graphqlFetchPlatform, remoteData, BNTApplicationResourceCenterServiceURL } = useModel("global");
    const { onCloseGlobalModal } = useModel("global.modal");
    const [form] = Form.useForm();

    const state = useReactive({
        loading: false,
        referenceDatabases: [],
        selectedData: null,
    });

    const loadReferenceDatabases = async () => {
        let res = await remoteData({
            object: "bnt-application-resource-center-service",
            serviceUrl: BNTApplicationResourceCenterServiceURL,
            serviceName: "/resource/referenceDatabase/organization/getAll",
            params: {},
        });
        state.referenceDatabases = res || [];
        state.selectedData = res?.find((item) => item.databaseName === record.itRedb);
    };

    useEffect(() => {
        loadReferenceDatabases();
    }, []);

    let onFinish = async () => {
        if (!state.selectedData) return;
        let query = `query MyQuery($itDbna: String!, $oiUuid: UUID!, $itUuid: UUID!) {
            a: allOrganizationIndustryTypes(
              condition: {itDbna: $itDbna, oiUuid: $oiUuid}
              filter: {itUuid: {notEqualTo: $itUuid}}
            ) {
              totalCount
            }
          }`;
        let res = await graphqlFetchPlatform({ query, variables: { oiUuid: record.oiUuid, itDbna: state.selectedData.graphqlServiceName, itUuid: record.itUuid } });
        if (res?.a?.totalCount > 0) return message.warning(`数据服务名称[${state.selectedData.graphqlServiceName}]已存在, 不能重复`);
        if (res?.a?.totalCount === 0) {
            res = await graphqlFetchPlatform({
                query: UpdateIndustryType,
                variables: {
                    oiUuid: record.oiUuid,
                    itUuid: record.itUuid,
                    itRedb: state.selectedData.databaseName,
                    itDbna: state.selectedData.graphqlServiceName,
                    itHbpa: state.selectedData.heartBeatParams,
                },
            });
            if (res) {
                message.success("成功修改信息");
                reloadData();
                onCloseGlobalModal();
            }
        }
    };

    return (
        <div>
            <Form form={form} layout="horizontal" onFinish={onFinish} labelCol={{ span: 5 }} wrapperCol={{ span: 17 }} initialValues={record}>
                <Form.Item label="行业类型名称">{record?.b?.itName}</Form.Item>

                <Form.Item label="参考数据库" name="itRedb" rules={[{ required: true, message: "请选择参数数据库" }]}>
                    <Select allowClear options={state.referenceDatabases} fieldNames={{ label: "title", value: "databaseName" }} onChange={(value, option) => (state.selectedData = option)} />
                </Form.Item>

                <div style={{ marginTop: 20, textAlign: "center" }}>
                    <Space>
                        <Button loading={state.loading} type="primary" htmlType="submit">
                            保存
                        </Button>
                        <Button loading={state.loading} onClick={onCloseGlobalModal}>
                            取消
                        </Button>
                    </Space>
                </div>
            </Form>
        </div>
    );
};

export default EditIndustryType;
